from collections import Counter
'''
重点：hash4,hash5,hash7
hash1:有效的字母异位词,a = "cat" , b = "tca" a,b是异位词，返回true
      使用字典key存a字母，value存次数，判断b是否在字典中存在，判断字典value是否<=0
hash2:两个数组的交集，nums1=[1,2,2,1] nums2=[2,2] 交集[2]
      字典判断
hash3:快乐数，递归
hash4:两数之和，返回下标,[2,7,11,15] target=9,返回[0,1]
      方法1：先排序，双指针求value，通过nums.index(value)反推下标
      重点方法：方法2：集合，注意for i ,value in enumerate(nums):得到下标和value，在判断target-value是否在集合中，若不在则加入，直到在集合中找到为止
hash5:四数相加，求集合个数，a [1,2] b [-2,-1] c [-1,2] d [0,2] 组成0的集合个数为2  [0,0,0,1][1,1,0,0]
      重点方法：同hash1,key 存a+b,value存次数，判断 0-(c+d)是否在key中，返回value次数
hash6:给你两个字符串：ransomNote 和 magazine ，判断 ransomNote 能不能由 magazine 里面的字符构成。
      输入：ransomNote = "aa", magazine = "aab"，输出：true
      记住 from collections import defaultdict 
      defaultdict(int)的用法，同hash1思路
hash7:三数之和 输入：nums = [-1,0,1,2,-1,-4] 输出：[[-1,-1,2],[-1,0,1]]
      双指针，此题不可用hash5方法，三数之和，固定一个数轮询，其他两个数双指针，时间复杂度n2
hash8:四数之和，同hash7，多加一层for循环
'''

'''
基础知识：
https://www.bilibili.com/video/BV1qW4y1a7fU?spm_id_from=333.788.videopod.episodes&vd_source=542bf208ec0df864ab17fe48fe242440&p=76
演示字典的常用操作
keys(), values(), items(), get(), pop(), popitem(), update(), setdefault(), clear(), copy(), fromkeys(
'''
my_dict = {"周杰伦":99,"林俊杰":88,"张学友":77}
# 新增元素
my_dict["张信哲"] = 66
print(f"新增后的字典dict:{my_dict}")
# 更新元素
my_dict["张信哲"] = 123
print(f"更新后字典dict:{my_dict}")
# 删除元素pop 返回value
score = my_dict.pop("周杰伦")
print(f"删除周杰伦后字典dict:{my_dict}，周杰伦的考试分数是：{score}")
# 删除元素popitem 返回删除的键值对元组()，按顺序先入后出，先删后入的键值对
score_item = my_dict.popitem()
print(f"score_item = my_dict.popitem(),{score_item}")
# 清空元素
my_dict.clear()
print("字典被清空了dict:{my_dict}")
# 获取全部key
my_dict = {"周杰伦":99,"林俊杰":88,"张学友":77}
keys = my_dict.keys() # 返回一个视图对象，显示字典中的键。
print(f"字典的全部keys是:{keys}")
# 遍历字典 支持for，不支持while
# 1. 遍历key
for key in keys:
    print(f"1字典的key:{key}")
    print(f"1字典的value:{my_dict[key]}")
# 2. 遍历字典本身
for key in my_dict:
    print(f"2字典的key:{key}")
    print(f"2字典的value:{my_dict[key]}")
# 统计字典元素数量，len()
print(f"字典元素数量是：{len(my_dict)}个")
print(f"get:{my_dict.get("a")}")
print(f"get:{my_dict.get("周杰伦")}")


c = Counter('abcdeabcdabcaba')  # count elements from a string

c.most_common(3)  # three most common elements
print("c.most_common(3) three most common elements [('a', 5), ('b', 4), ('c', 3)] : ",c.most_common(3)) #[('a', 5), ('b', 4), ('c', 3)]
sorted(c)  # list all unique elements
print("sorted(c)  list all unique elements ",c) #['a', 'b', 'c', 'd', 'e']
print("''.join(sorted(c.elements())) 'aaaaabbbbcccdde' : ",''.join(sorted(c.elements())))  # list elements with repetitions

print("sum(c.values()) 15:",sum(c.values()))  # total of all counts

'''
c['a']  # count of letter 'a'
5
for elem in 'shazam':  # update counts from an iterable
    ...
    c[elem] += 1  # by adding 1 to each element's count
c['a']  # now there are seven 'a'
7
del c['b']  # remove all 'b'
c['b']  # now there are zero 'b'
0

d = Counter('simsalabim')  # make another counter
c.update(d)  # add in the second counter
c['a']  # now there are nine 'a'
9

c.clear()  # empty the counter
c
Counter()


c = Counter('aaabbc')
c['b'] -= 2  # reduce the count of 'b' by two
c.most_common()  # 'b' is still in, but its count is zero
# [('a', 3), ('c', 1), ('b', 0)]
'''